package edu.stanford.bmir.protege.web.client.ui.ontology.stkos.enricher.currenttree;

import java.util.Collection;

import com.google.gwt.user.client.Timer;
import com.gwtext.client.core.Function;
import com.gwtext.client.widgets.tree.TreeNode;

import edu.stanford.bmir.protege.web.client.project.Project;
import edu.stanford.bmir.protege.web.client.rpc.data.EntityData;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.common.GWTExtUtil;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.common.StkosPanelType;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.enricher.common.StkosEnricherClassTreePanel;
import edu.stanford.bmir.protege.web.client.ui.ontology.stkos.enricher.common.StkosEnricherPropertyTreePanel;
import edu.stanford.bmir.protege.web.client.ui.portlet.AbstractOWLEntityPortlet;
import edu.stanford.bmir.protege.web.shared.hierarchy.ClassHierarchyParentAddedEvent;
import edu.stanford.bmir.protege.web.shared.hierarchy.ClassHierarchyParentAddedHandler;
import edu.stanford.bmir.protege.web.shared.hierarchy.DataPropertyHierarchyParentAddedEvent;
import edu.stanford.bmir.protege.web.shared.hierarchy.DataPropertyHierarchyParentAddedHandler;
import edu.stanford.bmir.protege.web.shared.hierarchy.ObjectPropertyHierarchyParentAddedEvent;
import edu.stanford.bmir.protege.web.shared.hierarchy.ObjectPropertyHierarchyParentAddedHandler;

public class StkosEnricherCurrentTreePortlet extends AbstractOWLEntityPortlet {

	private StkosEnricherClassTreePanel classTreePanel;

	private StkosEnricherPropertyTreePanel propertyTreePanel;

	public StkosEnricherCurrentTreePortlet(Project project) {
		super(project);
	}

	@Override
	public Collection<EntityData> getSelection() {
		return null;
	}

	@Override
	public void onRefresh() {
		if (isClassTreePanelActivity()) {
			classTreePanel.refreshTreeNode(getSelectedTreeNode());
		}
		if (isPropertyTreePanelActivity()) {
			propertyTreePanel.refreshTreeNode(getSelectedTreeNode());
		}
	}

	@Override
	public void reload() {
	}

	public boolean isClassTreePanelActivity() {
		boolean f = false;
		if (classTreePanel != null && !classTreePanel.isHidden()) {
			f = true;
		}
		return f;
	}

	public boolean isPropertyTreePanelActivity() {
		boolean f = false;
		if (propertyTreePanel != null && !propertyTreePanel.isHidden()) {
			f = true;
		}
		return f;
	}

	@Override
	public void initialize() {
		setHeight(500);

		showPanel(StkosPanelType.CLASS_TREE);

		addProjectEventHandler();
	}

	protected void addProjectEventHandler() {
		addProjectEventHandler(ClassHierarchyParentAddedEvent.TYPE,
				new ClassHierarchyParentAddedHandler() {
					@Override
					public void handleClassHierarchyParentAdded(
							final ClassHierarchyParentAddedEvent event) {
						if (isEventForThisProject(event)) {
							String parent = null, child = null;
							try {
								parent = event.getParent().getIRI().toString();
								child = event.getChild().getIRI().toString();
							} catch (Exception e) {
							}
							doHandler(parent, child);
						}
					}
				});
		addProjectEventHandler(DataPropertyHierarchyParentAddedEvent.TYPE,
				new DataPropertyHierarchyParentAddedHandler() {
					@Override
					public void handleDataPropertyParentAdded(
							final DataPropertyHierarchyParentAddedEvent event) {
						if (isEventForThisProject(event)) {
							String parent = null, child = null;
							try {
								parent = event.getParent().getIRI().toString();
								child = event.getChild().getIRI().toString();
							} catch (Exception e) {
							}
							doHandler(parent, child);
						}
					}
				});
		addProjectEventHandler(ObjectPropertyHierarchyParentAddedEvent.TYPE,
				new ObjectPropertyHierarchyParentAddedHandler() {
					@Override
					public void handleObjectPropertyHierarchyParentAdded(
							final ObjectPropertyHierarchyParentAddedEvent event) {
						if (isEventForThisProject(event)) {
							String parent = null, child = null;
							try {
								parent = event.getParent().getIRI().toString();
								child = event.getChild().getIRI().toString();
							} catch (Exception e) {
							}
							doHandler(parent, child);
						}
					}
				});
	}

	protected void doHandler(String parent, String child) {
		if (classTreePanel != null) {
			final TreeNode parentn = classTreePanel.getNodeById(parent);
			if (parentn != null) {
				Timer timer = new Timer(){
					@Override
					public void run() {
						parentn.expand();
					}
				};
				timer.schedule(450);
			}
		}
	}

	public StkosPanelType getActivityPanelType() {
		StkosPanelType type = null;
		if (isClassTreePanelActivity()) {
			type = StkosPanelType.CLASS_TREE;
		}
		if (isPropertyTreePanelActivity()) {
			type = StkosPanelType.PROPERTY_TREE;
		}
		return type;
	}

	public void showPanel(StkosPanelType type) {
		removeAll();
		if (StkosPanelType.CLASS_TREE.equals(type) && !isClassTreePanelActivity()) {
			if (classTreePanel == null) {
				addClassTreePanel();
			} else {
				add(classTreePanel);
				classTreePanel.show();
			}
		} else if (StkosPanelType.PROPERTY_TREE.equals(type)
				&& !isPropertyTreePanelActivity()) {
			if (propertyTreePanel == null) {
				addPropertyTreePanel();
			} else {
				add(propertyTreePanel);
				propertyTreePanel.show();
			}
		}
		updateTitle();
		doLayout();
	}

	protected void updateTitle() {
		setTitle("当前本体树");
		if (isClassTreePanelActivity()) {
			setTitle("当前本体类树");
		}
		if (isPropertyTreePanelActivity()) {
			setTitle("当前本体属性树");
		}
	}

	protected void addClassTreePanel() {
		classTreePanel = new StkosEnricherClassTreePanel(getProject());

		if (classTreePanel == null) {
			return;
		}

		classTreePanel.addListener(GWTExtUtil.EVENT_ONSELECTIONCHANGE,
				new Function() {

					@Override
					public void execute() {
						fireOnSelectionChangeEvent();
					}

				});
		classTreePanel.addListener(GWTExtUtil.EVENT_ONDBLCLICK, new Function() {

			@Override
			public void execute() {
				fireOnDblClickEvent();
			}

		});

		add(classTreePanel);
		doLayout();
	}

	protected void addPropertyTreePanel() {
		propertyTreePanel = new StkosEnricherPropertyTreePanel(getProject());

		if (propertyTreePanel == null) {
			return;
		}

		propertyTreePanel.addListener(GWTExtUtil.EVENT_ONSELECTIONCHANGE,
				new Function() {

					@Override
					public void execute() {
						fireOnSelectionChangeEvent();
					}

				});
		propertyTreePanel.addListener(GWTExtUtil.EVENT_ONDBLCLICK,
				new Function() {

					@Override
					public void execute() {
						fireOnDblClickEvent();
					}

				});

		add(propertyTreePanel);
		doLayout();
	}

	protected void fireOnSelectionChangeEvent() {
		fireEvent(GWTExtUtil.EVENT_ONSELECTIONCHANGE);
	}

	protected void fireOnDblClickEvent() {
		fireEvent(GWTExtUtil.EVENT_ONDBLCLICK);
	}

	public TreeNode getSelectedTreeNode() {
		TreeNode treeNode = null;
		if (isClassTreePanelActivity()) {
			treeNode = classTreePanel.getSelectionModel().getSelectedNode();
		}
		if (isPropertyTreePanelActivity()) {
			treeNode = propertyTreePanel.getSelectionModel().getSelectedNode();
		}
		return treeNode;
	}
}
